/**
 * 设备评价路由模块
 * routes/deviceFeedback.js
 */

const express = require("express");
const router = express.Router();
const deviceFeedback = require("../controller/deviceFeedback");
const auth = require("../controller/auth");

// 添加一个中间件来设置响应头
router.use((req, res, next) => {
  res.set({
    'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate',
    'Pragma': 'no-cache',
    'Expires': '0'
  });
  next();
});

/**
 * @swagger
 * /deviceFeedback/admin/all:
 *   get:
 *     summary: 管理员获取所有用户的评论信息
 *     tags: [DeviceFeedback]
 *     parameters:
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *       - in: query
 *         name: offset
 *         schema:
 *           type: integer
 *       - in: query
 *         name: orderBy
 *         schema:
 *           type: string
 *       - in: query
 *         name: sort
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: 获取所有用户评论成功
 *       401:
 *         description: 未登录或会话过期
 *       403:
 *         description: 无权限访问
 *       500:
 *         description: 服务器错误
 */
router.get("/admin/all", auth.isLogin, auth.isAdmin, deviceFeedback.getAdminFeedbackList);

/**
 * @swagger
 * /deviceFeedback/admin/{feedbackId}:
 *   delete:
 *     summary: 管理员删除评论
 *     tags: [DeviceFeedback]
 *     parameters:
 *       - in: path
 *         name: feedbackId
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 评论删除成功
 *       401:
 *         description: 未登录或会话过期
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 评论不存在
 *       500:
 *         description: 服务器错误
 */
router.delete("/admin/:feedbackId", auth.isLogin, auth.isAdmin, deviceFeedback.deleteFeedback);

/**
 * @swagger
 * /deviceFeedback/{deviceId}/feedback:
 *   get:
 *     summary: 获取设备评价列表
 *     tags: [DeviceFeedback]
 *     parameters:
 *       - in: path
 *         name: deviceId
 *         required: true
 *         schema:
 *           type: integer
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *       - in: query
 *         name: offset
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 获取评价列表成功
 *       500:
 *         description: 服务器错误
 */
router.get("/:deviceId/feedback", auth.isLogin, deviceFeedback.getFeedbackList);

/**
 * @swagger
 * /deviceFeedback/{deviceId}/rating:
 *   get:
 *     summary: 获取设备平均评分
 *     tags: [DeviceFeedback]
 *     parameters:
 *       - in: path
 *         name: deviceId
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 获取评分成功
 *       500:
 *         description: 服务器错误
 */
router.get("/:deviceId/rating", auth.isLogin, deviceFeedback.getAverageRating);

/**
 * @swagger
 * /deviceFeedback/{deviceId}/feedback:
 *   post:
 *     summary: 添加设备评价
 *     tags: [DeviceFeedback]
 *     parameters:
 *       - in: path
 *         name: deviceId
 *         required: true
 *         schema:
 *           type: integer
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - rating
 *               - feedback
 *             properties:
 *               rating:
 *                 type: integer
 *                 minimum: 1
 *                 maximum: 5
 *               feedback:
 *                 type: string
 *     responses:
 *       200:
 *         description: 添加评价成功
 *       400:
 *         description: 参数错误
 *       500:
 *         description: 服务器错误
 */
router.post("/:deviceId/feedback", auth.isLogin, deviceFeedback.addFeedback);

/**
 * @swagger
 * /deviceFeedback/{feedbackId}/like:
 *   post:
 *     summary: 点赞评价
 *     tags: [DeviceFeedback]
 *     parameters:
 *       - in: path
 *         name: feedbackId
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 点赞成功
 *       500:
 *         description: 服务器错误
 */
router.post("/:feedbackId/like", auth.isLogin, deviceFeedback.likeFeedback);

/**
 * @swagger
 * /deviceFeedback/{feedbackId}/dislike:
 *   post:
 *     summary: 踩评价
 *     tags: [DeviceFeedback]
 *     parameters:
 *       - in: path
 *         name: feedbackId
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 踩成功
 *       500:
 *         description: 服务器错误
 */
router.post("/:feedbackId/dislike", auth.isLogin, deviceFeedback.dislikeFeedback);

/**
 * @swagger
 * /deviceFeedback/{feedbackId}:
 *   put:
 *     summary: 更新设备评价
 *     tags: [DeviceFeedback]
 *     parameters:
 *       - in: path
 *         name: feedbackId
 *         required: true
 *         schema:
 *           type: integer
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               feedback:
 *                 type: string
 *               rating:
 *                 type: integer
 *                 minimum: 1
 *                 maximum: 5
 *     responses:
 *       200:
 *         description: 评价更新成功
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未登录或会话过期
 *       500:
 *         description: 服务器错误
 */
router.put("/:feedbackId", auth.isLogin, deviceFeedback.updateFeedback);

/**
 * @swagger
 * /deviceFeedback/{feedbackId}:
 *   delete:
 *     summary: 删除设备评价
 *     tags: [DeviceFeedback]
 *     parameters:
 *       - in: path
 *         name: feedbackId
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 评价删除成功
 *       401:
 *         description: 未登录或会话过期
 *       403:
 *         description: 无权删除此评论
 *       404:
 *         description: 评论不存在
 *       500:
 *         description: 服务器错误
 */
router.delete("/:feedbackId", auth.isLogin, deviceFeedback.deleteFeedback);

/**
 * @swagger
 * /deviceFeedback/user/{userId}:
 *   get:
 *     summary: 获取指定用户的评论信息
 *     tags: [DeviceFeedback]
 *     parameters:
 *       - in: path
 *         name: userId
 *         required: true
 *         schema:
 *           type: integer
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *       - in: query
 *         name: offset
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 获取用户评论成功
 *       401:
 *         description: 未登录或会话过期
 *       500:
 *         description: 服务器错误
 */
router.get("/user/:userId", auth.isLogin, deviceFeedback.getUserFeedback);

module.exports = router; 